How do I set up networking/WiFi/static IP address on Raspbian/Raspberry Pi OS?

您所在的位置:网站首页 wpa_supplicant conf How do I set up networking/WiFi/static IP address on Raspbian/Raspberry Pi OS?

How do I set up networking/WiFi/static IP address on Raspbian/Raspberry Pi OS?

#How do I set up networking/WiFi/static IP address on Raspbian/Raspberry Pi OS?| 来源: 网络整理| 查看: 265

This tutorial describes how to setup networking using the default network manager dhcpcd included in Raspbian since 2015-05-05 and Raspberry Pi OS. It applies to the Foundation releases of Raspberry Pi OS -Bullseye, Raspberry Pi OS -Buster, Raspbian Buster, Raspbian Stretch, Raspbian Jessie and the last Raspbian Wheezy. Bullseye & Buster settings are identical to Stretch.

How to setup Raspbian Networking

If you are using an Ethernet connection with a router there should be no configuration required and it should work out of the box.

The Pi3B+ and Pi4 have Gigabit Ethernet interfaces and will only work on a Gigabit Ethernet (1000BASE-T) router if a 8 wire CAT 5E cable is used. Earlier Pi models which only supported 100BASE-T work over 6 wire cable.

NOTE The Pi3/Pi Zero W inbuilt WiFi does not support 5GHz networks, and may not connect to Ch 12,13 on 2.4GHz networks until wireless regulatory domain is set.

WiFi on 5GHz enabled devices is disabled until wireless regulatory domain is set (Pi4B, Pi3B+, Pi3A+)

The domain can be set through Raspberry Pi Configuration (rc_gui), raspi-config or by setting country= to an appropriate ISO 3166 alpha2 country code in /etc/wpa_supplicant/wpa_supplicant.conf.

If you use the latest Raspberry Pi Imager there are advanced options, invoked by the magic key sequence: Ctrl-Shift-X. These enable you to set many initial options; hostname, username, password, ssh, WiFi Country Code etc during imaging. This should enable you to setup a headless system, without the need to edit any files.

If you are using WiFi and the GUI set up by following Using the Desktop

If you are using WiFi from the Command Line set up by following Using the Command Line This is the only way to set up a network which does not broadcast SSID.

This can also be used to setup a 'Headless' system e.g. using a serial console cable, but it MUCH easier if you can borrow a monitor and keyboard for setup. See below for another possible method using an Ethernet cable to a PC.

For most users READ NO FURTHER The following are Advanced options Headless Raspbian WiFi Setup

Raspbian, since May 2016, checks the contents of the boot directory for a file called wpa_supplicant.conf, and will copy the file into /etc/wpa_supplicant, replacing any existing wpa_supplicant.conf file that may be there. The file in the boot directory is then removed. This can be used to enable headless setup, using the wpa_supplicant.conf settings detailed below or Setting up a Raspberry Pi headless (You will probably want to enable ssh as well.)

NOTE the links to raspberrypi.org suggest using ifconfig - this is deprecated use ip a; ip r instead.

NOTE If you use the latest Raspberry Pi Imager the advanced options Ctrl-Shift-X make the above unnecessary.

What is my IP Address?

If you just want to know the IP Address your Pi is using enter hostname -I on the command line.

SSH

As of the November 2016 release, Raspbian has the SSH server disabled by default. You will have to enable it manually.

Enter sudo raspi-config in the terminal, first select advanced options, then navigate to ssh, press Enter and select Enable or disable ssh server.

For headless setup, SSH can be enabled by placing a file named 'ssh', without any extension, onto the boot partition of the SD card.

Networking Files

If you are running a recent Raspbian /etc/network/interfaces should be as below. If you have changed it PUT IT BACK. (Or on Stretch just delete it - it effectively does NOTHING.)

# interfaces(5) file used by ifup(8) and ifdown(8) # Please note that this file is written to be used with dhcpcd # For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf' # Include files from /etc/network/interfaces.d: source-directory /etc/network/interfaces.d

The file /etc/wpa_supplicant/wpa_supplicant.conf will be created/modified by the recommended setup methods, but can be setup by hand. Recent Raspbian have an option in raspi-config to enter SSID and Password. It should contain something like the following:-

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev update_config=1 country=AU network={ ssid="ESSID" psk="Your_wifi_password" }

If you need to connect to a private network (i.e. no broadcast SSID) include the line scan_ssid=1 inside network={⋯}.

NOTE If you want to connect to different networks (e.g. at work or home) you can include multiple network={⋯} entries.

If you have access to multiple networks and want to connect to a specific network, include the line priority=100 inside the network={⋯} entry. You can select a different priority for each network, the highest priority accessible network will be selected; the default is 0.

There are many other options which can be used see man wpa_supplicant.conf.

Notes on obsolete Operating Systems

Jessie does not include the 10-wpa_supplicant hook to manage WiFi interfaces, so links to wpa_supplicant are needed. These settings are incompatible with Predictable Network Interface Names.

The /etc/network/interfaces used by Jessie should be:-

# interfaces(5) file used by ifup(8) and ifdown(8) # Please note that this file is written to be used with dhcpcd # For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf' # Include files from /etc/network/interfaces.d: source-directory /etc/network/interfaces.d auto lo iface lo inet loopback iface eth0 inet manual allow-hotplug wlan0 iface wlan0 inet manual wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf allow-hotplug wlan1 iface wlan1 inet manual wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

NOTE calling dhcp in /etc/network/interfaces will disable dhcpcd.

Predictable Network Interface Names

Stretch introduced1 predictable network interface names which have been used in other distributions for some time.

Network interfaces will have names formed from a prefix en — Ethernet or wl — wlan followed by x indicating MAC and the MAC e.g. enxb827eb123456 or wlx00c140123456. The onboard WiFi of the Pi4, Pi3 and PIZeroW which is connected over sdio will however use the name wlan0

This can be beneficial to those using multiple network interfaces, however for most Pi users, with a single Ethernet and WiFi interface will make little difference. The previous names eth0 and wlan0 can be restored if you pass net.ifnames=0 on the kernel command line in /boot/cmdline.txt.

There is an option in raspi-config to enable predictable network interface names.

1. For some incomprehensible reason the Foundation decided to "Disable predictable network interface names for Ethernet devices" only 3 weeks after introducing the long-awaited feature. There is an option under Advanced Options of raspi-config to toggle this setting.

Setup a Static IP Address

If you want your Pi to be assigned a predictable IP Address you can either reserve one in your router OR request the DHCP server to assign one. E.g. the following will request an address on wlan0.

interface wlan0 request 10.1.2.99

request [address] Request the address in the DHCP DISCOVER message. There is no guarantee this is the address the DHCP server will actually give. If no address is given then the first address currently assigned to the interface is used.

If you request an IP Address within the range managed by the DHCP server which is available this should be honoured, otherwise the DHCP server will allocate an address as normal.

If you want an IP Address outside the range managed by the DHCP server e.g. if you have a range of addresses reserved use the inform directive.

inform address[/cidr[/broadcast_address]] This does not get a lease, just notifies the DHCP server of the address in use. You should also include the optional CIDR network number in case the address is not already configured on the interface.

If you REALLY need a static IP Address see separate answer Static IP Address

Advanced dhcpcd Configuration

Raspbian, by default, uses dhcpcd to manage network interfaces. This is automatic, and most users need do no more than specify the SSID and password for wireless networks.

It is possible to configure how dhcpcd works by entering options in /etc/dhcpcd.conf; see man dhcpcd.conf.There is a good article on dhcpcd at https://wiki.archlinux.org/index.php/dhcpcd

The following describe some of the more common configurations:-

Fallback profile

It is possible to configure a static profile within dhcpcd and fall back to it when DHCP lease fails. This is useful particularly for headless machines, where the static profile can be used as "recovery" profile to ensure that it is always possible to connect to the machine. The static profile is setup as any other Static IP Address

# define static profile profile static_eth0 static ip_address=⋯ static routers=⋯ static domain_name_servers=⋯ # fallback to static profile on eth0 interface eth0 fallback static_eth0 Prevent dhcpcd from configuring an interface

This is often done to enable the Pi to act as an Access Point (which needs to be configured using other files), while allowing normal DHCP configuration on other interfaces.

Add denyinterfaces wlan0 to the end of the file (but above any other added interface lines).

Prevent dhcpcd from configuring a gateway on an interface

If you want an interface to not install any default routes (often used in conjunction with a static IP address) specify

nogateway Host Specific Configuration

dhcpcd can configure interfaces dependent on the host network. This uses the Address Resolution Protocol (ARP) to probe hosts based on MAC or IP address before attempting DHCP resolution.

interface bge0 arping 192.168.0.1 # My specific 192.168.0.1 network profile dd:ee:aa:dd:bb:ee static ip_address=192.168.0.10/24 # A generic 192.168.0.1 network profile 192.168.0.1 static ip_address=192.168.0.98/24

You can speed up DHCP by disabling ARP probing if you are not using these features and have a simple network.

noarp Use different wpa_supplicant files

It is possible to configure dhcpcd to use different wpa_supplicant.conf files for a specific wireless interface.

Create a file named wpa_supplicant-"$interface".conf in /etc/wpa_supplicant/ e.g. wpa_supplicant-wlan0.conf will only be used by wlan0

/etc/wpa_supplicant/wpa_supplicant.conf will be used for any other wireless interfaces.

Connecting a Computer to the Pi

Recent versions of Raspbian (which use dhcpcd) allow ssh to work over a link-local address and avahi (which is a zeroconf implementation) enables programs to discover hosts running on a local network.

This means you can plug the Pi into a Computer (with an Ethernet cable) or a local network router and connect without knowing the IP address.

You can easily connect from Linux and OS X with ssh [email protected] (Prior to 2020-04-04 version of Bullseye the default username was 'pi', hostname raspberrypi. This is now set on initial boot.) This should work with popular GUI ssh programs. This is sometimes problematic with some versions of Windows and networks which use .local in a non-standard way. (See https://en.wikipedia.org/wiki/.local)

NOTE .local resolution does not always work e.g. in rsync. The following should resolve IP (and can be included in bash scripts) RemotePi=$(getent hosts hostname.local | awk '{ print $1 }')

If your system does not have getent to query the hosts e.g. macOS you can use the following:- RemotePi=$(arp -n hostname.local | awk '{x = $2; gsub(/[()]/, "", x); print x }')

If you have multiple Pi you need to make sure each Pi has a unique hostname.

You can use a crossover cable, but you don't need one (most modern interfaces automatically detect).

One drawback of direct connection is that the Pi will have no Internet access and the date will not be set. You can copy the date from the host by running ssh [email protected] sudo date -s$(date -Ins) before connection.

Scope:

This tutorial is about setting up a normal Raspbian/Raspberry Pi OS installation to access the internet using the included software. It also covers connections to other computers on the same network.

It is primarily aimed at helping new users struggling to get their Pi (especially WiFi) working .

It does NOT cover:-

Other Network Managers. Running advanced networking on the Pi (e.g. DHCP servers, Tunnelling, VPN, Access Point). Use of ipv6 (although if you have an ipv6 network this should work). Alternate networking setups. (There are many different ways of setting up networking.)


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3